﻿/*
 * jQuery object to handle interactions on the character pages.
 */
(function ($) {
    var settings = {
        updateSettingsUrl: '',
        updateSettingsCallback: null
    };

    var busy = false;

    var methods = {
        init: function (options) {
            settings = $.extend(settings, options);

            bindUpdateSettingsEvents();
        }
    };

    $.fn.user = function (method) {

        // Method calling logic
        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('Method ' + method + ' does not exist on user object.');
        }

    };

    function bindUpdateSettingsEvents() {
        // Handle the add adventure record item form submit.
        $('#editSettingsForm').submit(function (event) {
            event.preventDefault();

            if (busy)
                return;

            busy = true;

            var $this = $(this);

            var showAllSkills = $('#showAllSkills').is(':checked');

            var url = settings.updateSettingsUrl;

            url += '?showAllSkills=' + showAllSkills;

            doAjaxGet(url, null,
                function () {
                    if (settings.updateSettingsCallback)
                        settings.updateSettingsCallback();

                    busy = false;
                },
                function (data) {
                    showWarning(data);
                    busy = false;
                });
        });
    }
})(jQuery);